package com.hy.mvc.domain.dataobject.base;

import java.util.ArrayList;
import java.util.List;

/**
 * 分页DO
 * @author wb-hyl282156
 */
public class PageDO<T> extends BaseDO {

	private static final long serialVersionUID = 5514845906487473469L;

	/**
	 * 默认每页大小
	 */
	public static final Integer defaultPageSize = new Integer(10);
	/**
	 * 默认取第一页
	 */
	public static final Integer defaultFristPage = new Integer(1);
    /**
     * 默认总数为0
     */
	public static final Integer defaultTotleItem = new Integer(0);

    /**
     * 每页数量
     */
    private Integer pageSize;
    /**
     * 当前页数
     */
    private Integer currentPage;
    /**
     * 总数
     */
    private Integer totalItem;

    /**
     * 起始位置
     */
    private int startRow;

    /**
     * 截止位置
     */
    private int endRow;

    /**
     * 数据
     */
    private List<T> data;

    public List<T> getData() {
        return data;
    }

    public void setData(List<T> data) {
        this.data = data;
    }

    protected Integer getDefaultPageSize() {
        return defaultPageSize;
    }

    public boolean isFirstPage() {
        return this.getCurrentPage().intValue() == 1;
    }

    public boolean isLastPage() {
        return this.getTotalPage() == this.getCurrentPage().intValue();
    }

    public int getPreviousPage() {
        int back = this.getCurrentPage().intValue() - 1;
        if (back <= 0) {
            back = 1;
        }
        return back;
    }

    public int getNextPage() {
        int back = this.getCurrentPage().intValue() + 1;

        if (back > this.getTotalPage()) {
            back = this.getTotalPage();
        }

        return back;
    }

    public Integer getCurrentPage() {
        if (currentPage == null) {
            return defaultFristPage;
        }
        return currentPage;
    }

    public void setCurrentPage(Integer cPage) {
        if ((cPage == null) || (cPage.intValue() <= 0)) {
            this.currentPage = null;
        } else {
            this.currentPage = cPage;
        }
        this.setStartEndRow();
    }

    public int getTotalPage() {
        int pgSize = this.getPageSize().intValue();
        int total  = this.getTotalItem().intValue();
        int result = total / pgSize;

        if ((total == 0) || ((total % pgSize) != 0)) {
            result++;
        }

        return result;
    }

    public Integer getPageSize() {
        if (pageSize == null) {
            return getDefaultPageSize();
        }
        return pageSize;
    }

    public boolean hasSetPageSize() {
        return pageSize != null;
    }

    public void setPageSize(Integer pSize) {
        if ((pSize == null) || (pSize.intValue() <= 0)) {
            this.pageSize = null;
        } else {
            this.pageSize = pSize;
        }
    }

    public void setStartEndRow() {
        this.startRow = this.getPageSize().intValue() * (this.getCurrentPage().intValue() - 1);
        this.endRow = this.startRow + this.getPageSize().intValue() - 1;
    }

    public Integer getTotalItem() {
        if (totalItem == null) {
            return defaultTotleItem;
        }
        return totalItem;
    }

    public void setTotalItem(Integer tItem) {
        if (tItem == null) {
            throw new IllegalArgumentException("TotalItem can't be null!!");
        }

        this.totalItem = tItem;

        int current  = this.getCurrentPage().intValue();
        int lastPage = this.getTotalPage();

        if (current > lastPage) {
            this.setCurrentPage(new Integer(lastPage));
        }
    }

    public int getPageFirstItem() {
        int cPage = this.getCurrentPage().intValue();
        if (cPage == 1) {
            return 1; // 第一页开始当然是第 1 条记录
        }
        cPage--;
        int pgSize = this.getPageSize().intValue();
        return (pgSize * cPage) + 1;
    }

    public int getPageLastItem() {
        int cPage = this.getCurrentPage().intValue();
        int pgSize = this.getPageSize().intValue();
        int assumeLast = pgSize * cPage;
        int totalItem = getTotalItem().intValue();

        if (assumeLast > totalItem) {
            return totalItem;
        } else {
            return assumeLast;
        }
    }

    public int getEndRow() {
        return endRow;
    }

    public void setEndRow(int endRow) {
        this.endRow = endRow;
    }

    public int getStartRow() {
        return startRow;
    }

    public void setStartRow(int startRow) {
        this.startRow = startRow;
    }
    
    public List<String> getSlider() {
    	List<String> l = new ArrayList<String>(10);
    	int leftStart = 1;
    	int leftEnd = 2;
    	int mStart = this.getCurrentPage().intValue() - 2;
    	int mEnd = this.getCurrentPage().intValue() + 2;
    	int rStart = this.getTotalPage() - 1;
    	int rEnd = this.getTotalPage();
    	if (mStart <= leftEnd) {
    		leftStart = 0;
    		leftEnd = 0;
    		mStart = 1;
    	}
    	if (mEnd >= rStart) {
    		rStart = 0;
    		rEnd = 0;
    		mEnd = this.getTotalPage();
    	}
    	if (leftEnd > leftStart) {
    		for (int i = leftStart; i <= leftEnd; ++i) {
    			l.add(String.valueOf(i));
    		}
    		l.add("...");
    	}
    	for (int i = mStart; i <= mEnd; ++i) {
    		l.add(String.valueOf(i));
    	}
    	if (rEnd > rStart) {
    		l.add("...");
    		for (int i = rStart; i <= rEnd; ++i) {
    			l.add(String.valueOf(i));
    		}
    	}
    	return l;
    }

}
